<!doctype html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>添加品类</title>
    <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">
    <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/jquery.global.settings.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
    <script type="text/javascript" src="/webapp/js/common/config.js"></script>
</head>
<body>
<div class="easyui-container-fluid">
    <div class="easyui-panel" style="border: none;">
        <div class="nav" id="nav"></div>
        <div class="easyui-panel">
            <form id="ff" method="post">
                <div style="margin-bottom:20px">
                    <label class="item-label"><span class="item-required">*</span>名称：</label>
                    <input class="easyui-textbox" type="text" name="name" data-options="required:true"/>
                    <label class="item-label"><span class="item-required">*</span>类型：</label>
                    <select id="type" name="type" class="easyui-combobox" data-options="required:true,panelHeight:'100',prompt:'--请选择--'"  style="width:135px;">
                        <option value=""></option>
                        <option value="1">常规品类</option>
                        <option value="2">3C产品</option>
                    </select>
                    <label class="item-label">剔除品牌：</label>
                    <select id="brandSelect" name="tickBrandCode" class="easyui-combobox" style="width:350px;" data-options="multiple:true"></select>
                </div>
                <div style="margin-bottom:20px">
                    <label class="item-label">最大量：</label>
                    <input class="easyui-numberbox" type="text" name="countMax"/>
                    <label class="item-label">最小价：</label>
                    <input class="easyui-numberbox" type="text" name="priceMin"style="width:135px;"/>
                    <label class="item-label">品类简称：</label>
                    <input type="text" class="easyui-textbox" name="categoryAbbreviation"  />
                </div>
                <div style="margin-bottom:20px;color: red;">
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    注意：所有的匹配字不要带【品牌】信息,如：海信激光主机
                </div>
                <div style="margin-bottom:20px;">
                    <label class="item-label">匹配字：</label>
                    <input name="matchWords" class="easyui-tagbox" value="" style="width:50%;min-height:60px;" data-options="multiline:true"/>
                </div>
                <div style="margin-bottom:20px;">
                    <label class="item-label">天猫匹配字：</label>
                    <input name="tmMatch" class="easyui-tagbox" value="" style="width:50%;min-height:60px;" data-options="multiline:true"/>
                </div>
                <div style="margin-bottom:20px;">
                    <label class="item-label">禁用字：</label>
                    <input name="disableWords" class="easyui-tagbox" value="" style="width:50%;min-height:60px;" data-options="multiline:true"/>
                </div>
                <div style="margin-bottom:20px;">
                    <label class="item-label">天猫禁用字：</label>
                    <input name="tmDisable" class="easyui-tagbox" value="" style="width:50%;min-height:60px;" data-options="multiline:true"/>
                </div>
                <div id="moveDiv" style="margin-bottom:20px;display: none" >
                    <label class="item-label">转移字：</label>
                    <input name="moveWords" class="easyui-tagbox" value="" style="width:600px;min-height:60px;" data-options="multiline:true"/>
                    <label class="item-label">=></label>
                    <select class="easyui-combobox" id="moveCategoryCode" name="moveCategoryCode" style="width:135px;">
                    </select>
                </div>
                <div style="margin-bottom:20px;">
                    <label class="item-label">特殊规则：</label>
                    <input name="specialRules" id="specialRules" class="easyui-tagbox" value="" style="width:50%;min-height:60px;" data-options="multiline:true"/>
                </div>
                <div style="margin-bottom:20px;">
                    <label class="item-label">新款年限：</label>
                    <input type="text" class="easyui-textbox" name="newYears"  />
                </div>
                <div style="margin-left: 100px;margin-bottom: 20px;">
                    <a href="javascript:void(0)" class="easyui-linkbutton c8" onclick="submitForm()" style="width:80px">保存</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="cancel()" style="width:80px">取消</a>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
    var id = getQueryParameter("id");
    $(function(){
        setNav();
        $.when(initBrandSelect("brandSelect")).done(function(){initFormData()});
        if (id) {
            categoryMove();
            $("#moveDiv").show();
        }
    });
    function initFormData(){
        if(id){
            $.ajax({
                url:config.url +"/category/"+id,
                type:'get',
                data:{},
                xhrFields:{withCredentials:true},
                success:function(data){
                    //填充表单
                    if(data){
                        if(!data.matchWords){
                            data.matchWords = [];
                        }
                        if(!data.disableWords){
                            data.disableWords = [];
                        }
                        if (!data.tmDisable) {
                            data.tmDisable = [];
                        }
                        if (!data.tmMatch) {
                            data.tmMatch = [];
                        }
                        if (!data.specialRules) {
                            data.specialRules = [];
                        }
                        if (!data.tickBrandCode) {
                            data.tickBrandCode = [];
                        }
                        $("#ff").form("load",changeProperty2String(data));
                    }
                },
                error:function(){

                }
            });
        }
    }

    function submitForm() {
        $("#ff").form("submit", {
            onSubmit: function () {
                var valid = $(this).form('enableValidation').form('validate');
                if (!valid) return false;
                var formData = getFormData(document.getElementById("ff"));
                var method = "POST";
                if (id) {
                    method = "PATCH";
                    formData.id = id;
                }
                if (formData['matchWords'] && typeof formData['matchWords'] === 'string') {
                    formData['matchWords'] = [formData['matchWords']]
                }
                if (formData['disableWords'] && typeof formData['disableWords'] === 'string') {
                    formData['disableWords'] = [formData['disableWords']]
                }
                if(formData['tmMatch'] && typeof formData['tmMatch'] === 'string'){
                    formData['tmMatch'] = [formData['tmMatch']]
                }
                if(formData['tmDisable'] && typeof formData['tmDisable'] === 'string'){
                    formData['tmDisable'] = [formData['tmDisable']]
                }
                if (formData['moveWords'] && typeof formData['moveWords'] === 'string') {
                    formData['moveWords'] = [formData['moveWords']]
                }
                if (formData['tickBrandCode'] && typeof formData['tickBrandCode'] === 'string') {
                    formData['tickBrandCode'] = [formData['tickBrandCode']]
                }
                var bm = $("#specialRules").combobox('getValues');
                formData.specialRules = bm;
                $.ajax({
                    url: config.url + "/category",
                    type: method,
                    data: JSON.stringify(formData),
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    xhrFields: {withCredentials: true},
                    beforeSend: function () {
                        $.messager.progress({
                            title: "请稍等",
                            interval: 50,
                            text: "操作进行中..."
                        });
                    },
                    complete: function () {
                        $.messager.progress("close");
                    },
                    success: function (data) {
                        if (data.code == 515) {
                            $.messager.alert("提示", data.msg, "error");
                        }else {
                            $.messager.alert("提示", "操作成功", "info", function () {
                                window.parent.closeCurrentTab(true);
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, thrown) {
                        $.messager.alert("提示", "操作失败", "error");
                    }
                });
                return false;
            }
        });
    }
    function cancel(){
        window.parent.closeCurrentTab();
    }
    function categoryMove() {
        $.ajax({
            url: config.url + "/category/getAll",
            type: "get",
            xhrFields: {withCredentials: true},
            success: function (data) {
                if (data) {
                    data.unshift({"name": "", "code": ""});
                    $("#moveCategoryCode").combobox({
                        "textField": "name",
                        "valueField": "code",
                        "panelHeight": 150,
                        prompt:'--请选择--',
                        "data": data,
                        onChange: function (newValue, oldValue) {
                        }
                    });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        })
    }
</script>
</html>
